home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3 / CHAPTER2 / DRIVERS.C < prev    next >
C/C++ Source or Header  |  1996-04-27  |  7KB  |  215 lines

  1.  
  2. #include <windows.h>  
  3. #include "drivers.h"  
  4. #include "sqlext.h"
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "SQLDrivers()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.     WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109. static HENV hEnv  = NULL;
  110. static HWND hList = NULL;
  111.  
  112.    switch( uMsg )
  113.    {
  114.       case WM_CREATE  :
  115.               SQLAllocEnv( &hEnv );
  116.  
  117.               hList = CreateWindow( "LISTBOX", "",    
  118.                                     WS_CHILD | LBS_STANDARD | 
  119.                                     WS_VISIBLE | LBS_NOINTEGRALHEIGHT, 
  120.                                     0, 0, 
  121.                                     0, 0,  
  122.                                     hWnd,              
  123.                                     (HMENU)101,              
  124.                                     hInst,         
  125.                                     NULL               
  126.                                   );
  127.  
  128.               break;
  129.  
  130.       case WM_SIZE :
  131.               MoveWindow( hList, 0, 0, LOWORD( lParam ), HIWORD( lParam ), TRUE );
  132.               break; 
  133.               
  134.       case WM_COMMAND :
  135.               switch( LOWORD( wParam ) )
  136.               {
  137.                  case IDM_TEST :
  138.                         {
  139.                            UCHAR   szDriver[ 51 ];
  140.                            UCHAR   szAttributes[ 256 ];
  141.                            SWORD   wDriverLen;
  142.                            SWORD   wAttribLen;
  143.                            RETCODE retCode;
  144.  
  145.                            // Retrieve the first driver name.
  146.                            //................................
  147.                            retCode = SQLDrivers( hEnv, SQL_FETCH_FIRST, 
  148.                                                  szDriver, 51, &wDriverLen,
  149.                                                  szAttributes, 256, &wAttribLen );
  150.  
  151.                            // Continue retrieving driver names
  152.                            // until there are no more.
  153.                            //.................................
  154.                            while( retCode == SQL_SUCCESS || retCode == SQL_SUCCESS_WITH_INFO )
  155.                            {
  156.                               SendMessage( hList, LB_ADDSTRING, 0, (LPARAM)szDriver );
  157.  
  158.                               // retrieve the next driver name.
  159.                               //...............................
  160.                               retCode = SQLDrivers( hEnv, SQL_FETCH_NEXT, 
  161.                                                  szDriver, 51, &wDriverLen,
  162.                                                  szAttributes, 256, &wAttribLen );
  163.  
  164.                            }
  165.                         }
  166.                         break;
  167.  
  168.                  case IDM_ABOUT :
  169.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  170.                         break;
  171.  
  172.                  case IDM_EXIT :
  173.                         DestroyWindow( hWnd );
  174.                         break;
  175.               }
  176.               break;
  177.       
  178.       case WM_DESTROY :
  179.               PostQuitMessage(0);
  180.               SQLFreeEnv( hEnv );
  181.               break;
  182.  
  183.       default :
  184.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  185.    }
  186.  
  187.    return( 0L );
  188. }
  189.  
  190.  
  191. LRESULT CALLBACK About( HWND hDlg,           
  192.                         UINT message,        
  193.                         WPARAM wParam,       
  194.                         LPARAM lParam)
  195. {
  196.    static  HFONT hfontDlg;
  197.  
  198.    switch (message) 
  199.    {
  200.        case WM_INITDIALOG: 
  201.                return (TRUE);
  202.  
  203.        case WM_COMMAND:                              
  204.                if (   LOWORD(wParam) == IDOK         
  205.                    || LOWORD(wParam) == IDCANCEL)    
  206.                {
  207.                        EndDialog(hDlg, TRUE);        
  208.                        return (TRUE);
  209.                }
  210.                break;
  211.    }
  212.  
  213.    return (FALSE); 
  214. }
  215.